﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Draco.Commons.Container
{
    /// <summary>
    /// 三目字典结构
    /// </summary>
    public class TernaryDictionary
    {
        private Hashtable m_HashTable;

        /// <summary>
        /// 构造(忽略key的大小写)
        /// </summary>
        public TernaryDictionary()
            : this(StringComparer.CurrentCultureIgnoreCase){}
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="Comparer">Key比较选项</param>
        public TernaryDictionary(StringComparer Comparer)
        {
            m_HashTable = new Hashtable(Comparer);
        }
        /// <summary>
        /// 添加一项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="val2"></param>
        public void Add(string key,string val,string val2)
        {
            if(String.IsNullOrEmpty(key))
                throw new ArgumentNullException("key is empty");
            this.Add(new TernaryStruct(key, val, val2));
        }
        /// <summary>
        /// 添加一项
        /// </summary>
        /// <param name="Ternary"></param>
        public void Add(TernaryStruct Ternary)
        {
            this.m_HashTable[Ternary.Key] = Ternary;
        }

        /// <summary>
        /// 删除一项
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            if (!String.IsNullOrEmpty(key))
            {
                this.m_HashTable.Remove(key);
            }
        }
        /// <summary>
        /// 清除所有项
        /// </summary>
        public void Clear()
        {
            this.m_HashTable.Clear();
        }

        /// <summary>
        /// 获取数目
        /// </summary>
        public int Count { get { return m_HashTable.Count; } }

        /// <summary>
        /// 获取一项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TernaryStruct this[string key]
        {
            get
            {
                return m_HashTable[key] as TernaryStruct;
            }
        }

        /// <summary>
        /// 获取所有键名
        /// </summary>
        public List<String> Keys
        {
            get 
            {
                List<String> keys = new List<string>();
                foreach (String key in this.m_HashTable.Keys)
                    keys.Add(key);
                return keys;
            }
        }
    }
    /// <summary>
    /// 三目数据结构
    /// </summary>
    public class TernaryStruct
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 值2
        /// </summary>
        public string Value2 { get; set; }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="val2"></param>
        public TernaryStruct(string key, string val, string val2)
        {
            if (String.IsNullOrEmpty(key))
                throw new ArgumentNullException("key is empty");
            Key = key;
            Value = val;
            Value2 = val2;
        }
    }
}
